package cn.edu.zafu.bigdata.zyf;

import java.io.*;
import java.net.*;

public class Client {
    public static void main(String[] args) {
        String hostname = "localhost"; // 服务端的主机名或IP地址
        int port = 1234; // 服务端监听的端口
        try (Socket socket = new Socket(hostname, port);
             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {

            System.out.println("Client connected to server.");

            String userInput;
            while ((userInput = stdIn.readLine()) != null) {
                out.println(userInput); // 发送消息到服务端
                System.out.println("Server says: " + in.readLine()); // 接收服务端的回复
            }
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host " + hostname);
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}